package br.com.almoxarifadoporttek.persistence;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import br.com.almoxarifadoporttek.util.HibernateUtil;

import br.com.almoxarifadoporttek.model.Usuario;;

public class UsuarioDAO implements IUsuarioDAO{

	@Override
	public void save(Usuario usuario) 
	{
		Session session = null;
		try
		{
			session= HibernateUtil.getSession().openSession();
		
			Transaction t = session.beginTransaction();
			session.save(usuario);
			t.commit();
			
		}catch(Exception erro){			
			try
			{			
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();				
				}				
			} catch (Exception e) {
				
			}		
		}		
		finally 
		{
			session.close();	
		}
		
	}

	@Override
	public List<Usuario> list() 
	{
		
		Session session = null;
		
		try
		{
			session = HibernateUtil.getSession().openSession();
			Transaction t = session.beginTransaction();
			//List lista = session.createQuery("from Livro").list();		
			 
			List<Usuario> lista = session.createCriteria(Usuario.class).list();
			for (Usuario usuario: lista)
			{
				usuario.getNivel().getId();
			}
			t.commit();
			return lista;
			
		}
		catch(Exception erro)
		{
			
			try
			{			
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();
				}
			
				
			}
			catch (Exception e) 
			{
				
			}
			
			return null;			
		}
		
		finally 
		{
			session.close();		
		}	
		
	}

	@Override
	public void delete(Usuario usuario) {
		Session session = null;
		try
		{
			session= HibernateUtil.getSession().openSession();
		
			Transaction t = session.beginTransaction();
			session.delete(usuario);
			t.commit();
			
		}catch(Exception erro){			
			try
			{			
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();				
				}				
			} catch (Exception e) {
				
			}		
		}		
		finally 
		{
			session.close();	
		}
		
	}
	@Override
	public void update(Usuario usuario) 
	{
		Session session = null;
		try
		{
			session= HibernateUtil.getSession().openSession();
		
			Transaction t = session.beginTransaction();
			session.update(usuario);
			t.commit();
			
		}catch(Exception erro){			
			try
			{			
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();				
				}				
			} catch (Exception e) {
				
			}		
		}		
		finally 
		{
			session.close();	
		}
		
	}

	@Override
	public List<Usuario> findByNomeSenha(String usuario, String senha) {
	Session session = null;
			
			try
			{
					session = HibernateUtil.getSession().openSession();
					Transaction t = session.beginTransaction();
					Criteria criteria = session.createCriteria(Usuario.class);
					criteria.add(Restrictions.eq("usuario", usuario));
					criteria.add(Restrictions.eq("senha", senha));
					List<Usuario> lista =(List<Usuario>) criteria.list();
					for(Usuario user: lista)
					{
						user.getNivel().getId();
					}
					
					
					
					t.commit();
					return lista;
			}
			catch(Exception erro)
			{
			
				try
				{
					if(session != null)
					{
						if(session.getTransaction().isActive())
						session.getTransaction().rollback();
					}
				} 
				catch (Exception e) {}
				return null;
			
			}
	finally 
	{
		session.close();
	}
	}

	@Override
	public Usuario findByUsuario(String usuario) 
	{
		Session session = null;
		
		try
		{
				session = HibernateUtil.getSession().openSession();
				Transaction t = session.beginTransaction();
				Criteria criteria = session.createCriteria(Usuario.class);
				criteria.add(Restrictions.eq("usuario", usuario));
				Usuario user = (Usuario) criteria.list().get(0);
				t.commit();
				return user;
		}
		catch(Exception erro)
		{
		
			try
			{
				if(session != null)
				{
					if(session.getTransaction().isActive())
					session.getTransaction().rollback();
				}
			} 
			catch (Exception e) {}
			return null;
		
		}
		finally 
		{
			session.close();
		}
	}
}
